package com.hrms.hrms_workflow.config;

import com.hrms.hrms_workflow.listener.GlobalFlowableEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.ProcessEngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

@Configuration
public class FlowableConfig implements ApplicationListener<ContextRefreshedEvent> {
    @Bean
    public GlobalFlowableEventListener globalFlowableEventListener() {
        return new GlobalFlowableEventListener();
    }

    @Autowired
    private SpringProcessEngineConfiguration configuration;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        dispatcher.addEventListener(
                globalFlowableEventListener(),
                FlowableEngineEventType.TASK_CREATED,
                FlowableEngineEventType.TASK_COMPLETED
        );
    }
}